<template>
    <div class="login-container">
      <el-card class="login-card">
        <template #header>
          <div class="login-header">
            <h2>欢迎登录</h2>
          </div>
        </template>
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          label-width="80px"
          @keyup.enter="handleLogin"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              clearable
            />
          </el-form-item>
          
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
              clearable
            />
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              @click="handleLogin"
              style="width: 120px;"
            >
              登录
            </el-button>
            <el-button
              @click="toRegister"
              style="width: 120px;"
            >
              注册
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue'
  import { useRouter } from 'vue-router'
  import { useUserStore } from '@/stores/user'
  import { ElMessage } from 'element-plus'
  
  const router = useRouter()
  const userStore = useUserStore()
  
  // 表单数据
  const loginForm = ref({
    username: '',
    password: ''
  })
  
  // 表单验证规则
  const loginRules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
    ]
  }
  
  const loading = ref(false)
  const loginFormRef = ref(null)
  
  // 处理登录
  const handleLogin = async () => {
    try {
      // 表单验证
      await loginFormRef.value.validate()
      loading.value = true
      
      // 调用登录方法
      const success = await userStore.login(loginForm.value)
      if (success) {
        ElMessage.success('登录成功')
      }
    } catch (error) {
      console.error('登录失败:', error)
    } finally {
      loading.value = false
    }
  }
  
  // 跳转到注册页面
  const toRegister = () => {
    router.push('/register')
  }
  
  // 检查是否已登录
  onMounted(async () => {
    const isLogin = await userStore.checkLogin()
    if (isLogin) {
      router.push('/')
    }
  })
  </script>
  
  <style scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f7fa;
  }
  
  .login-card {
    width: 450px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .login-header {
    text-align: center;
    padding: 10px 0;
  }
  
  .login-header h2 {
    margin: 0;
    color: #409eff;
  }
  </style>